/**
 * WangSong 2009-10-17 增加getSql接口
 */
package cn.biplam.common.client;

import java.rmi.RemoteException;
import java.util.ArrayList;

import cn.biplam.common.interfaces.OperatorServer;
import cn.biplam.common.paramters.PersisParam;
import cn.biplam.common.paramters.PersisParam.Cache_Type;
import cn.biplam.common.tools.RMITOOLS;
import cn.biplam.common.wrapper.ErrorMessage;

/**
 * 持久化对象操作
 * @author 	WangSong
 * @create 	2009-3-11 下午04:46:17
 * @version 1.0
 */
public abstract class Operator {
	
	public static final String persisId="_persisId";
	/** 返回结果条数限制字符串*/
	public static final String lineLimit="_lineLimit";
	
	/**
	 * 执行Operator,返回数据访问结果
	 * @param opId    Operator id
	 * @param userId  用户id
	 * @param filter  过滤器对象
	 * @throws ErrorMessage 
	 */
	public static OperatorResult get(String opId, String userId,
			DataFilter filter) throws ErrorMessage {
		OperatorResult mOperatorResult = null;
		try {
			OperatorServer mOperatorServer = (OperatorServer)RMITOOLS.getInstance("OperatorServer");
			mOperatorResult = mOperatorServer.get(opId,userId,filter);
		} catch (ErrorMessage e) {
			throw e;
		} catch (Exception e) {
			throw new ErrorMessage(e,"Operator.get failed("+opId+") ");
		}
		if(mOperatorResult==null){
			throw new ErrorMessage("OperatorResult is null,OperatorID="+opId);
		}
		return mOperatorResult;
	}	
	
	/**
	 * 指定数据源,执行Operator,返回数据访问结果
	 * @param opId    Operator id
	 * @param userId  用户id
	 * @param filter  过滤器对象
	 * @param dbResource 数据源
	 * @throws ErrorMessage 
	 */
	public static OperatorResult get(String opId, String userId,
			DataFilter filter, DbResource dbResource) throws ErrorMessage {
		
		OperatorResult mOperatorResult = null;
		try {
			OperatorServer mOperatorServer = (OperatorServer)RMITOOLS.getInstance("OperatorServer");
			mOperatorResult = mOperatorServer.get(opId,userId,filter,dbResource);
		} catch (ErrorMessage e) {
			throw e;
		} catch (Exception e) {
			throw new ErrorMessage(e,"Operator.get failed("+opId+") ");
		}
		if(mOperatorResult==null){
			throw new ErrorMessage("OperatorResult is null,OperatorID="+opId);
		}
		return mOperatorResult;
	}

	/**
	 * 传入Sql容器,执行Operator,返回结果
	 * @param opId    Operator id
	 * @param userId  用户id
	 * @param filter  过滤器对象
	 * @param mDbResource 数据源
	 * @param sqls sql语句
	 * @return OperatorResult
	 * @throws RemoteException
	 */
	public static OperatorResult get(String opId,String userId,DataFilter filter,
			DbResource mDbResource,ArrayList<String> sqls) 
	                            throws ErrorMessage{
		OperatorResult mOperatorResult = null;
		try {
			OperatorServer mOperatorServer = (OperatorServer)RMITOOLS.getInstance("OperatorServer");
			mOperatorResult = mOperatorServer.get(opId,userId,filter,mDbResource,sqls);
		} catch (ErrorMessage e) {
			throw e;
		} catch (Exception e) {
			throw new ErrorMessage(e,"Operator.get failed("+opId+") ");
		}
		if(mOperatorResult==null){
			throw new ErrorMessage("OperatorResult is null,OperatorID="+opId);
		}
		return mOperatorResult;
	}
	
	/**
	 * 返回Operator的Sql容器
	 * @param opId    Operator id
	 * @param userId  用户id
	 * @param filter  过滤器对象
	 * @return	ArrayList<String>
	 * @throws ErrorMessage
	 */
	public static ArrayList<String> getSql(String opId,String userId,DataFilter filter)throws ErrorMessage{
		return get(opId, userId, filter,null,true).getSqls();
	}
	
	/**
	 * 执行Operator(可设置仅返回Sql)
	 * @param opId    Operator id
	 * @param userId  用户id
	 * @param filter  过滤器对象
	 * @param ifgetsql    是否返回SQL(true:返回sql不执行;false:执行不返回sql)
	 * @return OperatorResult
	 * @throws RemoteException
	 */
	public static OperatorResult get(String opId,String userId,DataFilter filter,boolean ifgetsql) 
	                            throws ErrorMessage{
		return get(opId,userId,filter,null,ifgetsql);
	}
	
	/**
	 * 指定数据源,执行Operator(可设置仅返回Sql)
	 * @param opId    Operator id
	 * @param userId  用户id
	 * @param filter  过滤器对象
	 * @param mDbResource 数据源信息
	 * @param ifgetsql    是否返回SQL(true:返回sql不执行;false:执行不返回sql)
	 * @return OperatorResult
	 * @throws RemoteException
	 */
	public static OperatorResult get(String opId,String userId,DataFilter filter,
			DbResource mDbResource,boolean ifgetsql) 
	                            throws ErrorMessage{
		OperatorResult mOperatorResult = null;
		try {
			if(filter!=null){
				filter.setIfGetSql(ifgetsql);
			}
			OperatorServer mOperatorServer = (OperatorServer)RMITOOLS.getInstance("OperatorServer");
			mOperatorResult = mOperatorServer.get(opId,userId,filter,mDbResource,ifgetsql);
		} catch (ErrorMessage e) {
			throw e;
		} catch (Exception e) {
			throw new ErrorMessage(e,"Operator.get failed("+opId+") ");
		}
		if(mOperatorResult==null){
			throw new ErrorMessage("OperatorResult is null,OperatorID="+opId);
		}
		return mOperatorResult;
	}
	
	/**
	 * 指定数据源,执行Operator(可设置持久层刷新方式)
	 * @param opId    Operator id
	 * @param userId  用户id
	 * @param filter  过滤器对象
	 * @param dbResource 数据源
	 * @param pageId 页面id
	 * @param cacheType Cache_Type cacheType是否刷新持久层（从数据库取数据，刷新持久层） refresh:刷新 lazy:不刷新
	 * @throws ErrorMessage 
	 */
	public static OperatorResult get(String opId, String userId,
			DataFilter filter, DbResource dbResource,String pageId,Cache_Type cacheType) throws ErrorMessage {
		
		OperatorResult mOperatorResult = null;
		try {
			OperatorServer mOperatorServer = (OperatorServer)RMITOOLS.getInstance("OperatorServer");
			mOperatorResult = mOperatorServer.get(opId,userId,filter,dbResource,pageId,cacheType);
		} catch (ErrorMessage e) {
			throw e;
		} catch (Exception e) {
			throw new ErrorMessage(e,"Operator.get failed("+opId+") ");
		}
		if(mOperatorResult==null){
			throw new ErrorMessage("OperatorResult is null,OperatorID="+opId);
		}
		return mOperatorResult;
	}	
	
	/**
	 * 取分页信息
	 * @param opId      持久化id
	 * @param userId    用户id
	 * @param filter    过滤器
	 * @param pageNum   页码
	 * @return
	 * @throws ErrorMessage 
	 */
	public static OperatorResult getPage(String opId, 
								     String userId,
								     DataFilter filter) throws ErrorMessage{
		OperatorResult mOperatorResult = null;
		try {
			filter.put(".queryModel", "page");
			OperatorServer mOperatorServer = (OperatorServer)RMITOOLS.getInstance("OperatorServer");
			mOperatorResult = mOperatorServer.getPage(opId, userId, filter);
		} catch (ErrorMessage e) {
			throw e;
		} catch (Exception e) {
			throw new ErrorMessage(e,"Operator.get failed("+opId+") ");
		}
		if(mOperatorResult==null){
			throw new ErrorMessage("OperatorResult is null,OperatorID="+opId);
		}
		return mOperatorResult;
	}
	
	/**
	 * 取分页信息
	 * @param opId      持久化id
	 * @param userId    用户id
	 * @param filter    过滤器
	 * @param pageNum   页码
	 * @param showPageLine 每页显示条数
	 * @return
	 * @throws ErrorMessage 
	 */
	public static OperatorResult getPage(String opId, 
								     String userId,
								     DataFilter filter,
								     int showPageLine) throws ErrorMessage{
		OperatorResult mOperatorResult = null;
		try {
			filter.put(".queryModel", "page");
			OperatorServer mOperatorServer = (OperatorServer)RMITOOLS.getInstance("OperatorServer");
			mOperatorResult = mOperatorServer.getPage(opId, userId, filter,showPageLine);
		} catch (ErrorMessage e) {
			throw e;
		} catch (Exception e) {
			throw new ErrorMessage(e,"Operator.get failed("+opId+") ");
		}
		if(mOperatorResult==null){
			throw new ErrorMessage("OperatorResult is null,OperatorID="+opId);
		}
		return mOperatorResult;
	}
	
	/**
	 * 设置持久化对象
	 * @param userId 用户id
	 * @param pageId 页面id
	 * @param opId  操作id
	 * @param object 持久化对象
	 * @throws ErrorMessage 
	 */
	public static void set(String userId,String pageId,String opId,OperatorResult object) throws ErrorMessage{
		checkParams(userId, opId);
		//OperatorServer mOperatorServer = (OperatorServer)RMITOOLS.getInstance("OperatorServer");
		try {
			PersisParam.set(userId,pageId, opId, object);
		}catch(ErrorMessage e){
			throw e;
		} catch (Exception e) {	
			throw new ErrorMessage(e,"Operator.get failed("+opId+") ");
		}
	}
	
	/**
	 * 设置持久化对象
	 * @param userId 用户id
	 * @param pageId 页面id
	 * @param opId  操作id
	 * @param persisId 持久化id (记录id)
	 * @param object 持久化对象
	 * @throws ErrorMessage 
	 */
	public static void set(String userId,String pageId,String opId,String persisId,OperatorResult object) throws ErrorMessage{
		checkParams(userId, opId);
		try {
			PersisParam.set(userId,pageId, opId,persisId,object);
		}catch(ErrorMessage e){
			throw e;
		} catch (Exception e) {	
			throw new ErrorMessage(e,"Operator.get failed("+opId+") ");
		}
	}
	
	/**
	 * 清除除用户的，页面下的，操作id下的持久化对象
	 * @param userId 用户id
	 * @param pageId 页面id
	 * @param optId  操作id
	 * @param persistId 持久化id
	 */
	public static void clear(String userId,String pageId,String optId,String persistId){
		PersisParam.clear(userId, pageId, optId, persistId);
	}
	
	private static void checkParams(String userId,String opId) throws ErrorMessage{
		if(userId==null){
			throw new ErrorMessage("UserId is null ");
		}
		if(opId==null){
			throw new ErrorMessage("OperatorID is null ");
		}
	}
}
